<template>
	<view>
		<view class="wrap">
			<view class="order-header">
				<view class="u-tabs-box">
					<u-tabs-swiper activeColor="#10beca" ref="tabs"
						:list="list" :current="current" @change="change" :is-scroll="false" swiperWidth="750">
					</u-tabs-swiper>
				</view>
				<view class="u-p-30 bg-white">
					<u-search v-model="key" :show-action="false" shape="square" clearabled @change="searchKeyword"></u-search>
				</view>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item class="swiper-item" v-for="(list,i) in orderList" :key="i">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<u-cell-group class="u-m-b-20" v-if="orderList[i].length > 0">
							<u-cell-item v-for="(res, index) in orderList[i]" :key="res.id" :use-label-slot="true" @click="details(res)">
								<view slot="title">
									<!-- <u-tag :text="res.state" type="primary" size="mini" /> -->
									<text>{{res.number}}</text>
								</view>
								<view slot="label">
									<view><text>建单时间：{{res.createddatetime}}</text></view>	
									<view><text>移库站点：{{res.store.name}}</text></view>
									<view><text>负责小哥：{{res.brother.name}}({{res.brother.mobile}})</text></view>
								</view>
								<view slot="right-icon">
									<u-tag :text="res.state" mode="plain" size="mini" />
								</view>
							</u-cell-item>
						</u-cell-group>
						<u-empty v-else text="您还没有相关的订单" mode="list"></u-empty>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [
					{
						value: '',
						name: '全部'
					},
					// {
					// 	value: 0,
					// 	name: '待出库'
					// },
					{
						value: 1,
						name: '已装车'
					},
					// {
					// 	value: 2,
					// 	name: '已移库'
					// },
					{
						value: 3,
						name: '已完结'
					},
					{
						value: 4,
						name: '已取消'
					}
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore'],
				PageIndex: 1,
				PageSize: 10,
				noMore: false,
				key: '',
				orderList: [
					[
						// {
						// 	commission: 0,
						// 	createddatetime: "2023-02-03 13:52:11",
						// 	fine: 0,
						// 	id: 5,
						// 	number: "BO2023020313520001",
						// 	orderamount: 0,
						// 	state: "待入库",
						// 	userorderamount: 960
						// },
						// {
						// 	commission: 0,
						// 	createddatetime: "2023-01-31 10:42:16",
						// 	fine: 0,
						// 	id: 4,
						// 	number: "BO2023013110420001",
						// 	orderamount: 0,
						// 	state: "待入库",
						// 	userorderamount: 1700
						// }
					],
					[],
					[],
					[],
					[],
					[]
				]
			}
		},
		onLoad(options) {
			console.log(options.current)
			if(options.current>0 || options.current == 0 ){
				this.current = options.current
				this.swiperCurrent = options.current
			}
		},
		onShow() {
			this.orderList[this.swiperCurrent] = []
			this.PageIndex = 1
			this.getOrderList(this.swiperCurrent);
		},
		onPullDownRefresh() {
			this.orderList[this.swiperCurrent] = []
			this.PageIndex = 1
			this.getOrderList(this.swiperCurrent).then(() => {
				uni.stopPullDownRefresh(); //停止当前页面下拉刷新
			})
		},
		methods: {
			// 查看详情
			details(item){
				uni.navigateTo({
					url: '../views/views?id='+item.id
				})
			},
			reachBottom() {
				// 此tab为空数据
				this.PageIndex += 1
				if (!this.noMore) {
					this.getOrderList(this.swiperCurrent).then((row) => {
						if (row.length == 0) {
							this.noMore = true
						}
					})
				}
			},
			// 页面数据
			getOrderList(idx) {
				this.loadStatus.splice(this.swiperCurrent, 1, "loading")
				
				// /Store/OutOrder/List
				//  /Store/TransferOrder/List 
				return this.$u.post('/Store/MoveOrder/List', {
					"PageIndex": this.PageIndex,
					"PageSize": this.PageSize,
					"key": this.key,
					"state": this.list[this.swiperCurrent].value
				}).then((res) => {
					if (res.rows.length > 0) {
						res.rows.forEach((item) => {
							this.orderList[idx].push(item)
						})
						if (res.rows.length < this.PageSize) {
							this.loadStatus.splice(this.swiperCurrent, 1, "nomore")
						} else {
							this.loadStatus.splice(this.swiperCurrent, 1, "loadmore")
						}
					} else {
						this.loadStatus.splice(this.swiperCurrent, 1, "nomore")
					}
					this.$forceUpdate()
					return res.rows
				})
			},
			// tab栏切换
			change(index) {
				this.PageIndex = 1;
				this.swiperCurrent = index;
				this.orderList[index] = [];
				this.noMore = false;
				this.getOrderList(index);
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
			
				if (current != this.current) { //滑动切换到其他分类时
					this.current = current;
					this.orderList[this.current] = []
					this.loadStatus.splice(this.current, 1, "loadmore")
					this.getOrderList(this.current)
				} else {
					this.current = current;
				}
			},
			searchKeyword(val){
				this.key = val
				this.PageIndex = 1;
				this.orderList[this.swiperCurrent] = [];
				this.getOrderList(this.swiperCurrent);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}
	
	.swiper-box {
		flex: 1;
	}
	
	.swiper-item {
		height: 100%;
	}
</style>
